<?php

namespace Common\Model;

use Think\Model;

class CouponModel extends Model {

    protected $_auto = array(/* array(完成字段1,完成规则,[完成条件,附加规则]) */
        array('addTime', 'time', 1, 'function'),
        array('updateTime', 'time', 2, 'function'),
        array('isOn', '1', 1),
        array('status', '0', 1),
    );  // 自动完成定义

    /**
     * 添加
     */

    public function addCoupon($data = '') {
        $rt = false;
        if ($this->create($data)) {
            $this->startTrans();
            $rt = $this->add();
            if ($rt) { // 添加操作记录
                $logData = array(
                    "couponID" => $rt,
                    "operatorID" => I("session.adminid"),
                    "manType" => 0,
                    "operate" => 1,
                );
                if (D("CouponDealLog")->addCouponDealLog($logData)) {
                    $this->commit();
                } else {
                    $rt = false;
                    $this->rollback();
                }
            } else {
                $this->rollback();
            }
        }
        return $rt;
    }

    /**
     * 更新(注意$data一定要有主键id,若$data为空，则默认是对$_POST数组处理)
     */
    public function updateCoupon($data = '') {
        $rt = false;
        if ($this->create($data)) {
            $this->startTrans();
            $rt = $this->save();

            if ($rt) { // 添加操作记录
                $logData = array(
                    "couponID" => $rt,
                    "operatorID" => I("session.adminid"),
                    "manType" => 0,
                    "operate" => 4,
                );
                if (D("CouponDealLog")->addCouponDealLog($logData)) {
                    $this->commit();
                } else {
                    $rt = false;
                    $this->rollback();
                }
            } else {
                $this->rollback();
            }
        }
        return $rt;
    }

    /**
     * 删除(平台商删除)
     */
    public function deleteCouponForAdmin($id) {
        $data = array(
            'couponID' => $id,
            'isOn' => 0,
            'updateTime' => time()
        );
        $this->startTrans();
        $rt = $this->save($data);
        
        if ($rt) { // 添加操作记录
            $logData = array(
                "couponID" => $rt,
                "operatorID" => I("session.adminid"),
                "manType" => 0,
                "operate" => 3,
            );
            if (D("CouponDealLog")->addCouponDealLog($logData)) {
                $this->commit();
            } else {
                $rt = false;
                $this->rollback();
            }
        } else {
            $this->rollback();
        }
        
        return $rt;
    }

    /**
     * 删除(用户删除)
     */
    public function deleteCouponForUser($id) {
        $data = array(
            'couponID' => $id,
            'userIsOn' => 0,
            'updateTime' => time()
        );
        $this->startTrans();
        $rt = $this->save($data);
        
        if ($rt) { // 添加操作记录
            $logData = array(
                "couponID" => $rt,
                "operatorID" => I("session.adminid"),
                "manType" => 1, /*用户*/
                "operate" => 3,
            );
            if (D("CouponDealLog")->addCouponDealLog($logData)) {
                $this->commit();
            } else {
                $rt = false;
                $this->rollback();
            }
        } else {
            $this->rollback();
        }
        
        return $rt;
    }

    /**
     *  获取一条记录
     */
    public function getCoupon($id, $where = array()) {
        if (!empty($id)) {
            $where['couponID'] = $id;
        }

        $result = $this->where($where)->find();

        return $result;
    }

    /**
     * 获得列表（带基本查询）
     */
    public function getCouponList($pageNo = 1, $pageSize = 15, $where = array(), $order = array(), $field = "*") {
        $rt = $this->field($field)->where($where)->page("$pageNo,$pageSize")->order($order)->select();
        return $rt;
    }

    public function getCouponListLength($where = array()) {
        return $this->where($where)->count();
    }

}

?>